package com.lancoo.ccas53.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName TimeCodeUtil
 * @Description TODO
 * @Author quin
 * @Date 2023/3/15 17:15
 */
public class TimeCodeUtil {

    public static final List<String> DAY_CODE = new ArrayList<>(Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14"));
    public static final List<String> DAY_NAME = new ArrayList<>(Arrays.asList("一", "二", "三", "四", "五", "六", "七", "一", "二", "三", "四", "五", "六", "七"));
    public static final List<String> DAY_NAME_BIG_WEEK = new ArrayList<>(Arrays.asList("大周1", "大周2", "大周3", "大周4", "大周5", "大周6", "大周7", "大周8", "大周9", "大周10", "大周11", "大周12", "大周13", "大周14"));
    public static final List<String> DAY_NAME_WEEK = new ArrayList<>(Arrays.asList("周1", "周2", "周3", "周4", "周5", "周6", "周7"));
    public static final List<String> NUMBER_LIST = Arrays.asList("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
    public static final List<String> UNIT_LIST = Arrays.asList("", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千");

    /**
     * 根据timeCode位数不同，采取不同转换方式
     *
     * @param timeCode 01、a01、01a01
     * @return 对应的文字
     */
    public static String convert(String timeCode) {
        if (timeCode == null) return null;
        switch (timeCode.length()) {
            case 5:
                return convertByTimeCode(timeCode);
            case 3:
                return convertByDayCode(timeCode);
            case 2:
                return convertByWeek(timeCode);
            case 4:
                return convertByWeekAndDay(timeCode);
            default:
                return "";
        }
    }

    /**
     * 四位timeCode转换
     *
     * @param timeCode 0101
     * @return 星期x第x节
     */
    private static String convertByWeekAndDay(String timeCode) {
        int week = Integer.parseInt(timeCode.substring(0, 2));
        int classPeriod = Integer.parseInt(timeCode.substring(2));
        return "周" + week + "第" + classPeriod + "节";
    }

    /**
     * 五位timeCode转换
     *
     * @param timeCode 01a01
     * @return 第xx周星期x第x节
     */
    private static String convertByTimeCode(String timeCode) {
        int week = Integer.parseInt(timeCode.substring(0, 2));
        String dayName = DAY_NAME.get(DAY_CODE.indexOf(timeCode.substring(2, 3)));
        int period = Integer.parseInt(timeCode.substring(3));
        return "第" + intToChineseNum(week) + "周" + dayName + "第" + intToChineseNum(period) + "节";
    }

    /**
     * 3位timeCode转换
     *
     * @param timeCode a01
     * @return 第xx周星期x第x节
     */
    private static String convertByDayCode(String timeCode) {
        String dayName = DAY_NAME.get(DAY_CODE.indexOf(timeCode.substring(0, 1)));
        int period = Integer.parseInt(timeCode.substring(1));
        return dayName + "第" + intToChineseNum(period) + "节";
    }

    /**
     * 2位timeCode转换
     *
     * @param timeCode 01
     * @return 第xx周星期x第x节
     */
    private static String convertByWeek(String timeCode) {
        int week = Integer.parseInt(timeCode);
        return "第" + intToChineseNum(week) + "周";
    }

    /**
     * 数字转汉字
     *
     * @param number 数字
     * @return 汉字
     */
    public static String intToChineseNum(int number) {
        StringBuilder dst = new StringBuilder();
        int count = 0;
        while (number > 0) {
            dst.insert(0, (NUMBER_LIST.get(number % 10) + UNIT_LIST.get(count)));
            number = number / 10;
            count++;
        }
        return dst.toString().replaceAll("^一十", "十").replaceAll("零[千百十]", "零")
                .replaceAll("零+万", "万").replaceAll("零+亿", "亿")
                .replaceAll("亿万", "亿零").replaceAll("零+", "零")
                .replaceAll("零$", "");
    }
}
